<template>
  <div class="TabPane">

    <el-form v-show="true" ref="queryForm" :model="artFrom" :inline="true" label-width="68px">
      <el-form-item label="商品分类">
        <el-select
          v-model="artFrom.category"
          placeholder="请选择"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option label="区域一" value="shanghai" />
          <el-option label="区域二" value="beijing" />
        </el-select>
      </el-form-item>

      <el-form-item label="商品搜索">
        <el-input
          v-model="artFrom.search"
          placeholder="请输入商品名称,关键字,ID"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        >
          <el-button slot="append" type="success" icon="el-icon-search" />
        </el-input>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <router-link
          :to="'/product/add_product'"
        >
          <el-button type="primary" plain icon="el-icon-plus" size="mini">添加商品</el-button>
        </router-link>

      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain size="mini">采集按钮</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button plain size="mini">批量下架</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button size="mini" icon="el-icon-upload2">导出</el-button>
      </el-col>
    </el-row>

    <div class="tab-pane-table">
      <div class="table">
        <el-table v-loading="loading" :data="tableData" style="width: 100%">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column prop="mer_Id" label="商品Id" width="80" />
          <el-table-column label="商品图" width="80">
            <template slot-scope="scope">
              <div class="tabBox_img">
                <img :src="scope.row.image || ''" :alt="scope.row.title">
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="title" label="商品名称" width="240" />
          <el-table-column label="商品售价" width="80">
            <template slot-scope="scope">
              <p>{{ scope.row.price | formatQuota }}</p>
            </template>
          </el-table-column>
          <el-table-column prop="sales" label="销量" width="80" />
          <el-table-column prop="stock" label="库存" sortable width="80" />
          <el-table-column prop="sort" label="排序" width="80" />
          <el-table-column label="状态" width="100">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.specType"
                active-value="0"
                inactive-value="1"
              />
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="220" align="center">
            <template slot-scope="scope">
              <el-button type="text" size="mini" @click="handleClick(scope.row)">查看</el-button>
              <el-button type="text" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
              <router-link :to="{ path: '/product/product_reply/'}">
                <el-button type="text" size="mini">查看评论</el-button>
              </router-link>
              <el-button type="text" size="mini">移到回收站</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="paging">
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getProduct"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { getProducts } from '@/api/product'
import Pagination from '@/components/Pagination'

export default {
  name: 'TabPane',
  components: {
    Pagination
  },
  filters: {
    // 价格过滤器
    formatQuota(val) {
      var toFixedNum = Number(val).toFixed(3)
      var realVal = toFixedNum.substring(0, toFixedNum.toString().length - 1)
      return realVal
    }
  },
  data() {
    return {
      value: '',
      artFrom: {
        category: '',
        search: ''
      },
      // 列表数据
      tableData: [],
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        Fields: ''
      }
    }
  },
  created() {
    this.getProduct()
  },
  methods: {
    // 修改产品操作
    handleUpdate(row) {
      this.$router.push({ path: '/product/add_product/' + row.id })
    },
    // 获取产品信息
    getProduct() {
      this.loading = true
      getProducts(this.queryParams).then(res => {
        this.tableData = res.data
        this.total = res.head.totalCount
        this.loading = false
      })
    }
  }
}
</script>

<style lang="scss" scope>
  .TabPane {
    .tab-pane-table {
      margin-top: 20px;
      .paging {
        margin-top: 20px;
        display: flex;
        justify-content: flex-end;
      }
      .table {
        .tabBox_img {
          width: 36px;
          height: 36px;
          cursor: pointer;
          img {
            max-width: 100%;
            max-height: 100%;
          }
        }
      }
    }
  }

  .el-table {
    .el-table__header-wrapper, .el-table__fixed-header-wrapper {
      th {
        word-break: break-word;
        background-color: #f8f8f9;
        color: #515a6e;
        height: 40px;
        font-size: 13px;
      }
    }
  }
</style>
